<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input
          v-model="query.nameCn"
          clearable
          placeholder="优惠券名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <date-range-picker
          v-model="modelEndDate"
          start-placeholder="優惠券开始时间"
          end-placeholder=""
          range-separator=""
          :fix-date.sync="query.endDate"
          class="date-item"
        />
        <date-range-picker
          v-model="modelStartDate"
          start-placeholder="優惠券结束时间"
          end-placeholder=""
          range-separator=""
          :fix-date.sync="query.startDate"
          class="date-item"
        />
        <el-select
          v-model="query.couponStatus"
          class="filter-item"
          placeholder="請選擇優惠券是否過期"
          clearable
          @keyup.enter.native="crud.toQuery"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.true_false"
            :key="item.id"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="queryPermission">
        <template #left>
          <el-button type="primary" size="mini" class="filter-item" icon="el-icon-plus" @click="jumpAddEdit">新增
          </el-button>
        </template>
      </crudOperation>

      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        stripe
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="nameCn" label="优惠券名称" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ scope.row.nameCn }}</div>
            <div>{{ scope.row.nameEn }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" align="right" />
        <el-table-column prop="startTime" label="时间" width="200px">
          <template slot-scope="scope">
            <div>領取日期: {{ scope.row.enableTime }}</div>
            <div>開始時間: {{ scope.row.startTime }}</div>
            <div>結束時間: {{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="integral" label="需要积分" />
        <el-table-column prop="minPoint" label="使用门槛" />
        <el-table-column prop="publishCount" label="发行数量" />
        <el-table-column prop="receiveCount" label="领取数量" />
        <el-table-column prop="count" label="剩余数量" />
        <el-table-column prop="perLimit" label="限领张数" />
        <el-table-column prop="useCount" label="已使用数量" />
        <el-table-column prop="note" label="备注" />
        <el-table-column prop="useType" label="使用类型">
          <template slot-scope="scope">
            {{ dict.label.useType[scope.row.useType] }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','coupon:edit','coupon:del'])" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-button
              v-permission="['admin', 'coupon:Details']"
              size="mini"
              type="success"
              @click="couponDetails(scope.row)"
            >詳情
            </el-button>
            <el-button
              v-permission="['admin', 'coupon:edit']"
              size="mini"
              type="primary"
              @click="jumpAddEdit(scope.row)"
            >修改
            </el-button>
            <el-button
              v-permission="['admin', 'coupon:del']"
              size="mini"
              type="danger"
              @click="crud.doDelete(scope.row)"
            >刪除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCoupon from '@/api/sms/coupon'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import DateRangePicker from '@/components/DateRangePicker'
import crudOperation from '@crud/CRUD.operation'

const defaultForm = {
  id: null,
  nameCn: null,
  nameEn: null,
  amount: null,
  code: null,
  count: 0,
  couponType: 0,
  enableTime: null,
  startTime: null,
  endTime: null,
  integral: 0,
  memberLevel: 0,
  minPoint: null,
  perLimit: 0,
  publishCount: 0,
  receiveCount: 0,
  useCount: 0,
  note: null,
  useType: 0
}
export default {
  name: 'Coupon',
  components: { pagination, rrOperation, DateRangePicker, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['couponType', 'useType', 'true_false'],
  cruds() {
    return CRUD(
      {
        title: '优惠劵',
        url: 'coupon/page',
        sort: 'id:desc',
        crudMethod: { ...crudCoupon },
        optShow: { del: true, download: true, reset: true },
        query: { couponType: 0, deleteStatus: false }
      })
  },
  data() {
    return {
      modelEndDate: '',
      modelStartDate: '',
      queryPermission: {
        del: ['admin', 'coupon:del']
      },
      permission: {
        del: ['admin', 'coupon:del']
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    jumpAddEdit(row) {
      let isEdit = false
      if (row.id) {
        isEdit = true
      }
      this.$router.push({ path: 'addCoupon/' + row.id, query: { id: row.id, isEdit: isEdit }})
    },
    couponDetails(row) {
      this.$router.push({ path: 'history/' + row.id, query: { id: row.id }})
    }
  }
}
</script>

<style scoped>

</style>
